﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace WenSkin.Controls
{
    public class WenToolStrip : ToolStrip
    {
        public WenToolStrip() : base()
        {
            base.ImageScalingSize = new Size(32, 32);
            base.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
            this.RenderMode = ToolStripRenderMode.System;
            BackColor = Color.Transparent;
        }

        private Color backColor;


        [DefaultValue(typeof(Size), "32,32")]
        public new Size ImageScalingSize { get => base.ImageScalingSize; set => base.ImageScalingSize = value; }

        [DefaultValue(typeof(Color), "Transparent")]
        public new Color BackColor
        {
            get => backColor;
            set
            {
                backColor = value;
                base.BackColor = value;
                this.Invalidate();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            ControlHelper.SetGDIHigh(g);
            if (this.RenderMode == ToolStripRenderMode.System)
            {
                Rectangle rect = new Rectangle(0, 0, this.Width - 2, this.Height - 2);
                e.Graphics.SetClip(rect);
            }
            base.OnPaint(e);
        }

    }


    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All), DefaultEvent("ValueChanged")]
    public class ToolStripDateTimePicker : ToolStripControlHost
    {
        private readonly DateTimePicker mDateTimePicker;

        [Category("Wen"), Description("日期格式选择")]
        public DateTimePickerFormat Format { get => mDateTimePicker.Format; set => mDateTimePicker.Format = value; }
        [Category("Wen"), Description("自定义格式样式")]
        public string CustomFormat { get => mDateTimePicker.CustomFormat; set => mDateTimePicker.CustomFormat = value; }
        [Category("Wen"), Description("获取时间值")]
        public DateTime Value { get => mDateTimePicker.Value; set => mDateTimePicker.Value = value; }
        public ToolStripDateTimePicker() : base(new DateTimePicker())
        {
            mDateTimePicker = DateTimePicker;
            mDateTimePicker.ValueChanged += MDateTimePicker_ValueChanged;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public DateTimePicker DateTimePicker
        {
            get { return (DateTimePicker)Control; }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override string Text
        {
            get { return string.Empty; }
            set { base.Text = string.Empty; }
        }

        public event EventHandler ValueChanged;

        private void MDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            ValueChanged?.Invoke(sender, e);
        }
    }


    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All), DefaultEvent("ValueChanged")]
    [ToolboxItem("System.Windows.Forms.Design.AutoSizeToolboxItem")]
    public class ToolStripCheckBox : ToolStripControlHost
    {
        public ToolStripCheckBox() : base(new CheckBox())
        {
            CheckBox.CheckedChanged += CheckBox_CheckedChanged;
        }

        public event EventHandler CheckedChanged;
        private void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckedChanged?.Invoke(sender, e);
        }

        [Category("Wen"), Description("是否选中")]
        public bool Checked { get => CheckBox.Checked; set => CheckBox.Checked = value; }
        [Category("Wen"), Description("CheckBox")]
        public CheckBox CheckBox
        {
            get { return (CheckBox)Control; }
        }
    }

    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All), DefaultEvent("ValueChanged")]
    [Designer("System.Windows.Forms.Design.ToolStripItemDesigner")]
    public class ToolStripRadioButton : ToolStripControlHost
    {
        public ToolStripRadioButton() : base(new RadioButton())
        {
            RadioButton.Click += RadioButton_Click;
        }

        public new event EventHandler Click;
        private void RadioButton_Click(object sender, EventArgs e)
        {
            Click?.Invoke(sender, e);
        }

        [Category("Wen"), Description("是否选中")]
        public bool Checked { get => RadioButton.Checked; set => RadioButton.Checked = value; }
        public RadioButton RadioButton { get => (RadioButton)Control; }
    }
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All), DefaultEvent("ValueChanged")]
    public class ToolStripWenLableClickToTextBox : ToolStripControlHost
    {
        public ToolStripWenLableClickToTextBox() : base(new WenLableClickToTextBox())
        {
        }
        public WenLableClickToTextBox WenLableClickToTextBox { get => (WenLableClickToTextBox)Control; }
    }
}
